﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：
//
//创建标识： || 2011-04-01
//
//修改标识：
//修改描述：
//**********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ucToolBar : UserControl
    {
        private List<Item> items = new List<Item>();

        /// <summary>
        /// 节点集合
        /// </summary>
        public List<Item> Items
        {
            get { return items; }
            set { items = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ucToolBar()
        {
            InitializeComponent();
            this.BackColor = Color.Transparent;
        }

        private void ucToolBar_Load(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                foreach (Item item in this.Items)
                {
                    i++;
                    PictureBox img = new PictureBox();
                    img.BackgroundImage = item.Img;
                    img.Width = item.Img.Width;
                    img.Height = item.Img.Height;
                    img.Dock = DockStyle.Fill;
                    img.Tag = item;
                    Panel p = new Panel();
                    p.BackColor = Color.Transparent;
                    p.Size = img.Size;
                    p.Dock = DockStyle.Left;
                    p.Controls.Add(img);
                    p.Name = "plAuto" + i.ToString();
                    img.MouseEnter += new EventHandler(img_MouseEnter);
                    img.MouseLeave += new EventHandler(img_MouseLeave);
                    img.Click += new EventHandler(img_Click);
                    this.Controls.Add(p);
                    if (item.Checked)
                    {
                        p.BackgroundImage = Properties.Resources.nav_on;
                        this.Refresh(p);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                MessageBox.Show(string.Format("加载ucToolBar发生错误,原因：{0}", ex.Message), "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        void img_Click(object sender, EventArgs e)
        {
            PictureBox img = sender as PictureBox;
            Panel pl = img.Parent as Panel;
            Refresh(pl);
        }

        /// <summary>
        /// 刷新选择的图片显示
        /// </summary>
        /// <param name="pl">当前选中图片得所属Panel</param>
        private void Refresh(Panel pl)
        {
            pl.Tag = true;
            foreach (Control ctl in pl.Parent.Controls)
            {
                if (ctl is Panel && ctl.Name != pl.Name)
                {
                    if (ctl.Tag != null && Convert.ToBoolean(ctl.Tag))
                    {
                        ctl.Tag = false;
                        ctl.BackgroundImage = null;
                    }
                }
            }
        }

        void img_MouseLeave(object sender, EventArgs e)
        {
            PictureBox img = sender as PictureBox;
            Panel pl = img.Parent as Panel;
            if (pl.Tag == null || !Convert.ToBoolean(pl.Tag))
            {
                Panel p = img.Parent as Panel;
                p.BackgroundImage = null;
            }
        }

        void img_MouseEnter(object sender, EventArgs e)
        {
            PictureBox img = sender as PictureBox;
            Panel pl = img.Parent as Panel;
            Item it = img.Tag as Item;
            if (pl.Tag == null || !Convert.ToBoolean(pl.Tag))
            {
                Panel p = img.Parent as Panel;
                p.BackgroundImage = Properties.Resources.nav_on;
            }
        }

        /// <summary>
        /// 集合项
        /// </summary>
        [Serializable]
        public class Item
        {
            private Image _Img;

            /// <summary>
            /// 显示图片
            /// </summary>
            public Image Img
            {
                get { return _Img; }
                set { _Img = value; }
            }

            private object _tag;

            /// <summary>
            /// 自定义数据
            /// </summary>
            public object Tag
            {
                get { return _tag; }
                set { _tag = value; }
            }
            private bool _checked;

            /// <summary>
            /// 是否选中
            /// </summary>
            public bool Checked
            {
                get { return _checked; }
                set { _checked = value; }
            }

            /// <summary>
            /// 构造Item信息
            /// </summary>
            /// <param name="img">显示图片</param>
            /// <param name="tag">用户数据</param>
            /// <param name="check">是否选中</param>
            public Item(Image img, object tag, bool check)
            {
                this.Img = img;
                this.Tag = tag;
                this.Checked = check;
            }
        }
    }
}
